package com.bertie.smiles2iupac.ui;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import com.bertie.smiles2iupac.Iupac;
import com.bertie.smiles2iupac.Molecule;

@SuppressWarnings("serial")
public class SmilesToIupacConverterUI extends JPanel implements ActionListener {
	private final static String PROMPT = "Type in SMILES name and press button to convert to UIPAC";
	protected JButton button;
	protected JTextField smilesString;
	protected JTextField uipacString;
	protected JTextArea messagePanel;

	public SmilesToIupacConverterUI() {
		super(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();

		JLabel smilesLabel = new JLabel("Enter SMILES name here");
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridwidth = 2;
		c.weightx = 1.0;
		c.gridx = 0;
		c.gridy = 0;
		c.ipadx = 5;
		c.ipady = 5;
		add(smilesLabel, c);

		JLabel iupacLabel = new JLabel("IUPAC name");
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridwidth = 2;
		c.weightx = 1.0;
		c.gridx = 2;
		c.gridy = 0;
		c.ipadx = 5;
		c.ipady = 5;
		add(iupacLabel, c);

		smilesString = new JTextField("CCC(C)(C)C(CC)CCC");
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridwidth = 2;
		c.ipadx = 5;
		c.ipady = 5;
		c.weightx = 1.0;
		c.gridx = 0;
		c.gridy = 1;
		add(smilesString, c);

		uipacString = new JTextField();
		uipacString.setEditable(false);
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridwidth = 2;
		c.ipadx = 5;
		c.ipady = 5;
		c.weightx = 1.0;
		c.gridx = 2;
		c.gridy = 1;
		add(uipacString, c);

		button = new JButton("Convert SMILES to UIPAC");
		button.setVerticalTextPosition(AbstractButton.CENTER);
		button.setHorizontalTextPosition(AbstractButton.CENTER);
		button.addActionListener(this);
		button.setToolTipText("Click this button to convert SMILES to UIPAC.");
		c.fill = GridBagConstraints.CENTER;
		c.ipadx = 5;
		c.ipady = 5;
		c.gridwidth = 4;
		c.gridx = 0;
		c.gridy = 2;
		add(button, c);

		messagePanel = new JTextArea(PROMPT, 3, 75);
		messagePanel.setEditable(false);
		c.fill = GridBagConstraints.HORIZONTAL;
		c.ipadx = 5;
		c.ipady = 5;
		c.gridwidth = 4;
		c.weightx = 1.0;
		c.gridx = 0;
		c.gridy = 3;
		add(messagePanel, c);
		actionPerformed(null);
	}

	public void actionPerformed(ActionEvent e) {
		try {
			String iupach = Iupac.buildFromMolecule(Molecule.buildFromSmilesFormatString(smilesString.getText()));
			uipacString.setText(iupach);
			messagePanel.setText(PROMPT);
			messagePanel.setForeground(Color.BLACK);
		} catch (Exception ex) {
			messagePanel.setForeground(Color.RED);
			messagePanel.setText("Bad or Unsupported SMILES string :\r\n" + ex.getMessage());
			uipacString.setText("");
		}
	}

	private static void createAndShowGUI() {
		JFrame frame = new JFrame("Smiles To Iupac Converter");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		SmilesToIupacConverterUI newContentPane = new SmilesToIupacConverterUI();
		newContentPane.setOpaque(true);
		frame.setContentPane(newContentPane);
		frame.pack();
		frame.setVisible(true);
	}

	public static void main(String[] args) {
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				createAndShowGUI();
			}
		});
	}
}